package com.capgemini.teamtwo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.capgemini.teamtwo.common.BusinessException;
import com.capgemini.teamtwo.common.ExceptionEnum;
import com.capgemini.teamtwo.common.ResponseBean;
import com.capgemini.teamtwo.entity.ProjectInfo;
import com.capgemini.teamtwo.entity.User;
import com.capgemini.teamtwo.entity.dto.ProjectInfoDateDto;
import com.capgemini.teamtwo.entity.dto.ProjectInfoDto;
import com.capgemini.teamtwo.service.ProjectInfoService;
import com.capgemini.teamtwo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api("项目管理")
@RestController
@RequestMapping("/project")
public class ProjectManagementController extends BaseController{
    @Autowired
    private ProjectInfoService projectInfoService;
    @Autowired
    private UserService userService;
    @ApiOperation("新增项目")
    @PostMapping
    public ResponseBean save(@RequestBody ProjectInfoDateDto projectInfoDateDto){

        //end
        ProjectInfo projectInfo=new ProjectInfo();
        BeanUtils.copyProperties(projectInfoDateDto,projectInfo,"launchDate");
        if(projectInfoDateDto.getLaunchDate()!=null&&projectInfoDateDto.getLaunchDate()!="") {
            DateTimeFormatter dfer = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            projectInfo.setLaunchDate(LocalDate.parse(projectInfoDateDto.getLaunchDate(), dfer));

            boolean reasult = projectInfoService.saveAfterCalculate(projectInfo);
            log.info("currentProjectId:" + projectInfo.getId());
            return ResponseBean.success(projectInfo.getId());
        }
        return ResponseBean.failed("日期类型错误");
    }
    @ApiOperation("按条件查询项目列表")
    @GetMapping("/page")
    public ResponseBean<Page> list(Integer page, Integer pageSize, String projectName, String launchDate) throws ParseException {

        log.info("查询条件：");
        System.out.println(launchDate);//前端没串就是null
        System.out.println(projectName);
        Page<ProjectInfo> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<ProjectInfo> projectInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        projectInfoLambdaQueryWrapper.like(projectName!=null&&projectName!="",ProjectInfo::getProjectName,"%"+projectName+"%");
        if(launchDate!=null&&launchDate!="") {
            DateTimeFormatter dfer = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date = LocalDate.parse(launchDate, dfer);
            projectInfoLambdaQueryWrapper.eq(ProjectInfo::getLaunchDate, date);
        }
        projectInfoService.page(pageInfo,projectInfoLambdaQueryWrapper);
        log.info(pageInfo.getRecords().toString());
        //查询出来的项目信息只包含负责人id需要转换
        Page<ProjectInfoDto> projectInfoDto=new Page<>();
        BeanUtils.copyProperties(pageInfo,projectInfoDto,"records");
        List<Integer> leaderIds=pageInfo.getRecords().stream().map(item->{
            return item.getLeaderId();
        }).collect(Collectors.toList());
        List<User> entireTable=userService.listByIds(leaderIds);
        //test
        projectInfoDto.setRecords(pageInfo.getRecords().stream().map(item ->{
            ProjectInfoDto dto=new ProjectInfoDto();
            BeanUtils.copyProperties(item,dto,"leaderName");
            String leaderName = null;
            for(User leader:entireTable){
                if(leader.getId().equals(item.getLeaderId())) leaderName=leader.getRealname();
            }
            dto.setLeaderName(leaderName);
            return dto;
        }).collect(Collectors.toList()));
        //
        log.info(projectInfoDto.getRecords().toString());
        return ResponseBean.success(projectInfoDto);
    }
    @ApiOperation("按id删除项目")
    @DeleteMapping
    public ResponseBean delete( Integer id){
        log.info("delete被调用了");
        boolean result=projectInfoService.removeWithOtherTable(id);

        return ResponseBean.success(result);
    }
    @ApiOperation("更改项目基本信息")
    @PutMapping
    public ResponseBean update(@RequestBody ProjectInfo projectInfo){
        boolean result=projectInfoService.updateWithOtherTable(projectInfo);
        return ResponseBean.success(result);
    }
    @ApiOperation("查询空闲leader列表")
    @GetMapping("/leaders")
    @ResponseBody
    public List<User> leaderList(){
        LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getStatus,"Bench");
        userLambdaQueryWrapper.eq(User::getRank,"Leader");
        List<User> leaders=userService.list(userLambdaQueryWrapper);
        return leaders;
    }

}